<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>stdClass 类</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="closure.fromcallable.html">« Closure::fromCallable</a></li>
      <li style="float: right;"><a href="class.generator.html">Generator »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="reserved.interfaces.html">预定义接口和类</a></li>
    <li>stdClass 类</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.stdclass" class="reference">

 <h1 class="title">stdClass 类</h1>
 

 <div class="partintro"><p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p>

  <div class="section" id="stdclass.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    具有动态属性的通用空类。
   </p>

   <p class="para">
    此类的对象可以使用 <a href="language.oop5.basic.html#language.oop5.basic.new" class="link">new</a>
    运算符实例化，也可以通过<a href="language.types.object.html#language.types.object.casting" class="link">类型转换为对象</a>创建。几个
    PHP 函数也会创建此类的实例，比如
    <span class="function"><a href="function.json-decode.html" class="function">json_decode()</a></span>、<span class="function"><a href="mysqli-result.fetch-object.html" class="function">mysqli_fetch_object()</a></span>
    或 <span class="methodname"><a href="pdostatement.fetchobject.html" class="methodname">PDOStatement::fetchObject()</a></span>。
   </p>

   <p class="para">
    尽管没有实现 <a href="language.oop5.overloading.html#object.get" class="link">__get()</a>/<a href="language.oop5.overloading.html#object.set" class="link">__set()</a> 魔术方法，但此类允许动态属性且不需要
    <code class="code">#[\AllowDynamicProperties]</code> 属性。
   </p>

   <p class="para">
    这不是 PHP 的基类，因为 PHP 没有通用基类的概念。然而，可以创建继承
    <span class="classname"><strong class="classname">stdClass</strong></span> 的自定义类，从而继承动态属性的功能。
   </p>
  </div>

  <div class="section" id="stdclass.synopsis">
   <h2 class="title">类摘要</h2>

   <div class="classsynopsis">
    <div class="ooclass">
     
    </div>

    <div class="classsynopsisinfo">
     <span class="ooclass">
      <span class="modifier">class</span> <strong class="classname">stdClass</strong>
     </span>
     {</div>
   }</div>

   <p class="para">
    此类没有方法和默认属性。
   </p>
  </div>

  <div class="section" id="stdclass.examples">
   <h2 class="title">范例</h2>
   <div class="example" id="stdclass.basic-example">
    <p><strong>示例 #1 通过类型转换为对象创建</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$obj&nbsp;</span><span style="color: #007700">=&nbsp;(object)&nbsp;array(</span><span style="color: #DD0000">'foo'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">);</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(stdClass)#1 (1) {
  [&quot;foo&quot;]=&gt;
  string(3) &quot;bar&quot;
}
</pre></div>
    </div>
   </div>
   <div class="example" id="stdclass.json-example">
    <p><strong>示例 #2 通过 <span class="function"><a href="function.json-decode.html" class="function">json_decode()</a></span> 创建</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{"foo":"bar"}'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">));</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(stdClass)#1 (1) {
  [&quot;foo&quot;]=&gt;
  string(3) &quot;bar&quot;
}
</pre></div>
    </div>
   </div>
   <div class="example" id="stdclass.properties-example">
    <p><strong>示例 #3 声明动态属性</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$obj&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">stdClass</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">42</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;{</span><span style="color: #0000BB">1</span><span style="color: #007700">}&nbsp;=&nbsp;</span><span style="color: #0000BB">42</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">);</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(stdClass)#1 (2) {
  [&quot;foo&quot;]=&gt;
  int(42)
  [&quot;1&quot;]=&gt;
  int(42)
}
</pre></div>
    </div>
   </div>
  </div>

 </div>

</div>
</div></div></body></html>